package org.jack.bluetooth;


import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import org.jack.activity.R;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

//00:22:FD:29:91:AD
public class BluetoothClient
{
	private final static String TAG = BluetoothClient.class.getSimpleName();
	public final static int DISCOVERY = 0X1;
	private Handler handler = null;
	private BluetoothAdapter bluetooth = null;
	private Activity activity = null;
	private BluetoothSocket socket = null;
	private BluetoothDevice device = null;
	private ConnectThread connectThread = null;
	private boolean connected = false;
	
	public BluetoothClient(Context arg_context)
	{
		activity = (Activity) arg_context;
		handler = new Handler()
		{
			
			@Override
			public void handleMessage(Message msg)
			{
				Log.d(TAG, "Show message:" + msg.obj.toString());
				Toast.makeText(activity, msg.obj.toString(), Toast.LENGTH_SHORT).show();
			}
			
		};
		Log.d(TAG, "Get bluetooth...");
		bluetooth = BluetoothAdapter.getDefaultAdapter();
		if(null == bluetooth)
		{
			showWarning("当前设备上没有蓝牙");
			return;
		}
		Log.d(TAG, "Local address : " + bluetooth.getAddress());
		if (!bluetooth.isEnabled())
		{
			return;
		}
	}
	private void showWarning(String info)
	{
		Message msg = new Message();
		msg.obj = info;
		handler.sendMessage(msg);
	}
	public void connect()
	{
		try
		{
			Log.d(TAG, activity.getResources().getString(R.string.mac));
			Log.d(TAG, activity.getResources().getString(R.string.uuid));
			device = bluetooth.getRemoteDevice(activity.getResources().getString(R.string.mac));
		} catch (Exception e)
		{
			e.printStackTrace();
			showWarning("无法通过MAC获取远程设备");
			return;
		}
		connectThread = new ConnectThread();
		connectThread.start();
	}
	
	public void close()
	{
		new CloseThread().start();
	}
	
	public boolean sendMsg(int msg)
	{
		if (null == socket)
		{
			showWarning("没有连接服务端");
			return false;
		}
		OutputStream os = null;
		try
		{
			os = socket.getOutputStream();
			os.write(msg);
		} catch (IOException e)
		{
			e.printStackTrace();
			showWarning("无法发送数据:" + e.getMessage());
			return false;
		}
		return true;
	}
	public boolean isConnected()
	{
		return connected;
	}
	private class ConnectThread extends Thread
	{
		@Override
		public void run()
		{
			try
			{
				socket = device.createRfcommSocketToServiceRecord(UUID.fromString(
						activity.getResources().getString(R.string.uuid)));
				showWarning("正在连接服务...");
				socket.connect();
				showWarning("连接成功");
				connected = true;
				bluetooth.cancelDiscovery();
			} catch (IOException e)
			{
				e.printStackTrace();
				showWarning("无法连接服务,请检查服务端是否已正常启动");
			}
		}
		
	}
	private class CloseThread extends Thread
	{

		@Override
		public void run()
		{
			if (null != connectThread)
			{
				connectThread.interrupt();
				connectThread = null;
				connected = false;
				
			}
			if (null != socket)
			{
				try
				{
					socket.close();
				} catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				socket = null;
			}
		}
		
	}
}
